package com.heima.controller;

import com.heima.pojo.dto.PageQuerySku;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.domain.PageResult;
import com.heima.service.SkuService;
import com.heima.utils.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品操作类接口
 *
 */



@Api(value = "/api/vm-service/sku", tags = {"商品操作类接口"})
@RestController
@RequestMapping("/api/vm-service/sku")
public class SkuController {


    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @Autowired
    private SkuService skuService;


    @ApiOperation(value = "文件上传接口", httpMethod = "POST")
    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam("fileName")MultipartFile file)  {
        if(file.isEmpty()){
            return "file is empty";
        }
        String fileUrl = null;
        try {
            fileUrl = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            return fileUrl;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

//新增商品
    @PostMapping
    public Boolean addSku(@RequestBody SkuDto skuDto){
        if (skuDto == null) return false;
        Boolean res = skuService.insertSku(skuDto);
        return res;
    }


    //商品搜索
    @GetMapping("/search")
    public PageResult search(PageQuerySku pqr){
        PageResult pageResult = skuService.selectList(pqr.getPageIndex(),pqr.getPageSize(),pqr.getSkuName());
        return pageResult;
    }


    //修改商品
    @PutMapping("{skuId}")
    public Boolean update(@PathVariable Long skuId ,@RequestBody SkuDto skuDto){
        Boolean res = skuService.updateSku(skuId , skuDto);
        return res;
    }

}
